package telebasesimple;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import dblib.NewConnectDialog;
import dblib.DB;
import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TeleBaseSimple {

    public static void main(String[] args) {
        // Logger
        try {
            Logger.getLogger("").addHandler(new FileHandler("errorlog.xml"));
        } catch (IOException ex) {
            Logger.getLogger(TeleBaseSimple.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(TeleBaseSimple.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Look and feel
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(TeleBaseSimple.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Initialisation and Properties
        Vars.PropPath = System.getProperty("user.home") + "/.TeleBaseSimple";
        Vars.PropFileName = Vars.PropPath + "/Options.xml";
        File dir = new File(Vars.PropPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        Boolean NeedConnect = false;
        Vars.LoadProperties();

        if (!Vars.prop.containsKey("Username")) {
            NeedConnect = true;
        }
        if (!Vars.prop.containsKey("Userpass")) {
            NeedConnect = true;
        }
        if (!Vars.prop.containsKey("DB_URL")) {
            NeedConnect = true;
        }
        if (!NeedConnect) {
            DB.init(Vars.prop.getProperty("Username"), Vars.prop.getProperty("Userpass"), Vars.prop.getProperty("DB_URL"));
            if (!new DB().checkConnection()) {
                NeedConnect = true;
            }
        }

        if (NeedConnect) {
            NewConnectDialog newConnDlg = new NewConnectDialog(null, true);

            newConnDlg.AddSQL("CREATE TABLE IF NOT EXISTS people ("
                    + "k INT NOT NULL AUTO_INCREMENT, "
                    + "name VARCHAR(1024) NOT NULL, "
                    + "job VARCHAR(2048) NOT NULL, "
                    + "hide TINYINT(1) NOT NULL, "
                    + "PRIMARY KEY (k)) ENGINE = InnoDB");

            newConnDlg.AddSQL("CREATE TABLE IF NOT EXISTS `phones` ("
                    + "`k` INT NOT NULL AUTO_INCREMENT, "
                    + "`p_id` INT NOT NULL, "
                    + "`phone` VARCHAR(1024) NOT NULL, "
                    + "`hide` TINYINT(1) NOT NULL, "
                    + "PRIMARY KEY (`k`), "
                    + "INDEX `fk_phones_1` (`p_id` ASC), "
                    + "CONSTRAINT `fk_phones_1` FOREIGN KEY (`p_id`) REFERENCES `people` (`k`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB");

            newConnDlg.SetStrings(
                    "Для работы программы необходимо подключиться к серверу баз даных.",
                    "Все таблицы и настройки успешно созданы.");

            newConnDlg.setLocationRelativeTo(null);
            newConnDlg.setVisible(true);
            if (newConnDlg.getResult().equals("False")) {
                Runtime.getRuntime().exit(1);
            }
            Vars.prop.setProperty("Username", DB.getProp().getProperty("Username"));
            Vars.prop.setProperty("Userpass", DB.getProp().getProperty("Userpass"));
            Vars.prop.setProperty("DB_URL", DB.getProp().getProperty("DB_URL"));
            Vars.SaveProperties();
        } else {
            DB.init(Vars.prop.getProperty("Username"),
                    Vars.prop.getProperty("Userpass"),
                    Vars.prop.getProperty("DB_URL"));
        }
        // End of Initialisation and Properties

        MainFrame frm = new MainFrame();
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}
